class LevelController < ApplicationController
  #权限验证
  before_filter :auth
  layout "admin"
  def index
    redirect_to :action=>'list'
  end

  def new
    @level = Level.new
    @titles = "新增级别"
  end


  def create
    @level = Level.new(params[:level])
    level = Level.find_all_by_host @level[:host]
    if  @level && @level.save && !level.nil? && @level.errors.empty?
      flash[:notice] = '搞定一个级别！'
      Level.set_level_map
      redirect_to :action=>'list'
    else
      render :action => "new"
    end
  end


  #查询
  def list
    #提交表单为post
    if request.post?
      @level_search = params[:level]
      name = @level_search[:name]
      @levels = Level.paginate :per_page => $perPage, :page =>  params[:page],
        :conditions => ['name like ?', "%#{name}%"], :order => 'created_at desc'
    else
      @levels = Level.paginate  :page => params[:page],
        :per_page => $perPage,
        :order => "created_at desc"
    end
    #查询项参数
    @level = Level.new
    @level.name = name
    #功能名
    @titles = "级别列表"
  end

  def edit
    @level = Level.find(params[:id])
    @titles = "编辑级别---"+@level.name
  end

  def update
    @level = params[:level]
    if Level.update(@level[:id], @level)
      flash[:notice] = '更新成功'
      Level.set_level_map
      redirect_to :action=>'index'
    else
      render :action => "edit"
    end
  end

  def show
    @level = Level.find(params[:id])
    @titles = "级别详细信息---"+@level.name
  end

end
